## ************************************************************************** ## 
## Project: Gov 2020 Project
## Author: Amelia Malpas, Leah Stein & David Struhs
## Date Created: September 27, 2024
## Date Edited: November 27, 2024
## Task: DID Models and Regression Output
## ************************************************************************** ## 

## -------------------------------------------------------------------------- ## 
## Input: "final_data.Rdata"
## Output: DID Models and Regression Table
## -------------------------------------------------------------------------- ## 

## -------------------------------------------------------------------------- ## 
## Set up: load libraries, define paths 
## -------------------------------------------------------------------------- ## 

rm(list = ls())

library(tidyverse)
library(lfe)
library(fixest)
library(modelsummary)
library(scales)
library(ggplot2)
library(fixest)
library(scales)
library(knitr)
library(stringr)

load("data/final_data.Rdata")


chips_states <- c(
  "arizona", "colorado", "florida", "georgia", "idaho", "indiana",
  "minnesota", "missouri", "north carolina", "new hampshire", "new mexico",
  "new york", "ohio", "oregon", "texas", "utah", "vermont"
)


## -------------------------------------------------------------------------- ## 
## Restructure Data for Tables and Plots
## -------------------------------------------------------------------------- ## 


df_16_24 <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


state <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


mm <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0)) %>%
  filter(chips_mm == 1)

cz <- merged_result %>% 
  filter(state %in% chips_states) %>%
  select(state, county, county_fips, dem_vote_2016, dem_vote_2020, dem_vote_2024, chips_county_project, chips_county, chips_mm, chips_cz) %>%
  pivot_longer(cols = c(dem_vote_2016, dem_vote_2020, dem_vote_2024),
               names_to = "year",
               values_to = "dem_percent",
               names_prefix = "dem_vote_",
               names_transform = list(year = as.integer)) %>%
  filter(year == 2020 | year == 2024) %>%
  mutate(treatment = chips_county, 
         post = ifelse( year == 2024, 1, 0))


## -------------------------------------------------------------------------- ## 
## CZ within State Regression
## -------------------------------------------------------------------------- ## 


# Model 1: Simple Difference-in-Differences
czstate_did <- feols(dem_percent ~ chips_cz * post | state,
                     data = state,
                     cluster = ~ state)

# Summarize models for the 'cz' dataset
summary(czstate_did)



## -------------------------------------------------------------------------- ## 
## Commuting Zone within Media Market Regression
## -------------------------------------------------------------------------- ## 
# 

# Model 2: Simple Difference-in-Differences
czmm_did <- feols(dem_percent ~ chips_cz * post | state + chips_mm, 
                  data = mm) 

## -------------------------------------------------------------------------- ## 
## County Regression
## -------------------------------------------------------------------------- ## 
# 
county_did <- feols(dem_percent ~ treatment*post | state,
                    data = state, 
                    cluster = ~ state)

countymm_did <- feols(dem_percent ~ treatment*post | state,
                      data = mm, 
                      cluster = ~ state)

#output models
summary(czmm_did)
summary(czstate_did)
summary(countymm_did)

# Set the style first
setFixest_etable(style.tex = "aer")

# Now create the table
latex_output <- etable(county_did, countymm_did, czstate_did, czmm_did,
                       title = "Effects of CHIPS Act on Democratic Vote Share",
                       digits = 3,
                       se.below = TRUE,
                       depvar = TRUE,
                       notes = "Standard errors clustered at the commuting zone level.",
                       headers = c("County", "County", "Commuting Zone", "Commuting Zone"),
                       tex = TRUE,
                       fitstat = c('n'),
                       dict = c(chips_cz = "Chips CZ",
                                post = "Post",
                                treatment = "County"))

# Print the LaTeX code to the console
cat(latex_output)

